package studentSystem;

import java.util.ArrayList;
import java.util.Scanner;

import static studentSystem.Landing.landingMenu;

public class Menu {
    public static void startStudentSystem(ArrayList<User> listUser) {
        /*为学生管理系统书写一个登陆、注册、忘记密码的功能。
        只有用户登录成功之后，才能进入到学生管理系统中进行增删改查操作。*/
        ArrayList<Student> list = new ArrayList<>();
        chooseMenu(list,listUser);

    }

    public static void chooseMenu(ArrayList<Student> list,ArrayList<User> listUser) {
        System.out.println("----------欢迎来到学生管理系统----------");
        System.out.println("请选择操作(输入操作前的序号)");
        System.out.println("1、添加学生");
        System.out.println("2、删除学生");
        System.out.println("3、修改学生");
        System.out.println("4、查询学生");
        System.out.println("5、退出");
        System.out.println();
        System.out.println("您要选择的操作是：");

        Scanner sc = new Scanner(System.in);
        String i = sc.next();
        switch (i) {
            case "1":
                addStudent(list,listUser);
                break;
            case "2":
                deleStudent(list,listUser);
                break;
            case "3":
                changeStudent(list,listUser);
                break;
            case "4":
                checkStudent(list,listUser);
                break;
            case "5":
                System.out.println("欢迎与您再次相遇！");
                landingMenu(listUser);
                break;
            default:
                System.out.println();
                System.out.println("请输入一个正确的数字");
                chooseMenu(list,listUser);
                break;
        }
    }

    public static void addStudent(ArrayList<Student> list,ArrayList<User> listUser) {
        System.out.println("您的选择是：增加学生");
        Scanner sc = new Scanner(System.in);
        loop:
        while (true) {
            System.out.println("请输入学生的id：");
            String id = sc.next();
            for (int i = 0; i < list.size(); i++) {
                Student s1 = list.get(i);
                if (s1.getId().equals(id)) {
                    System.out.println();
                    System.out.println("该学生id已经被录入！");
                    System.out.println("请重新输入想要添加的学生id。");
                    System.out.println();
                    continue loop;
                }
            }
            System.out.println("请输入学生的姓名：");
            String name = sc.next();
            System.out.println("请输入学生的年龄：");
            int age = sc.nextInt();
            System.out.println("请输入学生的住址：");
            String address = sc.next();
            Student s2 = new Student(id, name, age, address);
            list.add(s2);
            break;
        }
        System.out.println();
        System.out.println("录入成功！");
        System.out.println();
        chooseMenu(list,listUser);
    }

    public static void deleStudent(ArrayList<Student> list,ArrayList<User> listUser) {
        System.out.println("您的选择是：删除学生");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要删除的学生id：");
        String id = sc.next();
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            if(s.getId().equals(id)){
                System.out.println("请按1确认删除(其余数字为不删除)：");
                int confirm = sc.nextInt();
                if (confirm == 1) {
                    list.remove(i);
                    System.out.println();
                    System.out.println("学生信息删除成功。");
                    chooseMenu(list,listUser);
                } else {
                    System.out.println();
                    System.out.println("删除失败");
                    chooseMenu(list,listUser);
                }
            }
        }
        System.out.println();
        System.out.println("该id不存在");
        chooseMenu(list,listUser);
    }

    public static void changeStudent(ArrayList<Student> list,ArrayList<User> listUser) {
        System.out.println("您的选择是：修改学生");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要修改的学生id：");
        String id = sc.next();
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            if(s.getId().equals(id)){
                System.out.println("请重新输入学生的姓名：");
                String name = sc.next();
                System.out.println("请重新输入学生的年龄：");
                int age = sc.nextInt();
                System.out.println("请重新输入学生的家庭地址：");
                String address = sc.next();
                Student news = new Student(id,name,age,address);
                list.set(i,news);
                System.out.println();
                System.out.println("修改成功。");
                chooseMenu(list,listUser);
            }
        }
        System.out.println();
        System.out.println("该id不存在");
        chooseMenu(list,listUser);
    }

    public static void checkStudent(ArrayList<Student> list,ArrayList<User> listUser) {
        System.out.println("您的选择是：查询学生");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您想要查询的学生id：");
        String id = sc.next();
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            if(s.getId().equals(id)){
                System.out.println();
                System.out.println("已查询到该学生信息");
                System.out.println("id\t\t姓名\t\t年龄\t\t家庭住址");
                System.out.print(s.getId() + "\t" + s.getName() + "\t");
                System.out.println(s.getAge() + "\t" + s.getAddress());
                chooseMenu(list,listUser);
            }
        }
        System.out.println();
        System.out.println("无当前学生信息，请添加后再查询。");
        System.out.println();
        chooseMenu(list,listUser);
    }
}
